#!/usr/bin/env python
# -*- coding: UTF-8 -*-

'''
简单字段对比过滤

usage:
field_contrast = {
    'tid_1'         :   'tid',
    'item_code_1'   :   'item_code',
    'logistics_no'  :   'out_sid',
}
data = {
    'logistics_no':'ttttt',
    'test':'demo',
    'tid_1':'',
}

eg1:
print contrast_filter(field_contrast,data,False,default={'tid':'aa'})
result:{'out_sid': 'ttttt'}

eg2:
print contrast_filter(field_contrast,data,True,default={'tid':'aa'})
result:{'tid': 'aa', 'out_sid': 'ttttt', 'item_code': ''}

'''
def contrast_filter(field,data,empty_retain=False,default={}) :
    result = {}
    field_keys = field.keys()
    for key in field_keys :
        target_key = field[key]     #field.get(key)
        if key and target_key :
            value = data.get(key)
            if value :
                result[target_key] = value
            elif empty_retain :
                result[target_key] = default.get(target_key,'')
    return result

